$(function () {
    // 列表加载
    query(page.pageIndex, page.pageSize);
    // 绑定事件
    $("#queryBtn").click(function () {
        query(page.pageIndex, page.pageSize);
    });
    var sizeInput = $("#pageSize");
    sizeInput.val(page.pageSize);// 赋初始值
    sizeInput.keydown(function (event) {
        AllowNumber(null, event);
    });
    sizeInput.blur(function () {
        page.pageIndex = 1;
        page.pageSize = sizeInput.val();
        query(page.pageIndex, page.pageSize);
    });
});

// 查询
function query(pageIndex, pageSize) {
    ajaxGet(url(pageIndex, pageSize), {}, function (data) {
        data = JSON.parse(data);
        if (data.success) {
            loadList(data.data.stuList);
            page = data.data.page;
            loadCatalogue();
        } else {
            alert("服务器正忙，查询失败");
        }
    });
}

// 加载学生列表
function loadList(stuList) {
    var html = "";
    for (var i = 0; i < stuList.length; i++) {
        var stu = stuList[i];
        html += "<tr>" +
            "<td align=\"center\">" + stu.number + "</td>" +
            "<td align=\"center\">" + stu.name + "</td>" +
            "<td align=\"center\">" + (stu.sex == "MALE" ? "男" : "女") + "</td>" +
            "<td align=\"center\">" + stu.phone + "</td>" +
            "<td align=\"center\">" + stu.profession + "</td>" +
            "<td align=\"center\">" + formatDate(new Date(stu.regDate)) + "</td>" +
            "<td align=\"center\">" +
            "<a href='edit.html?id=" + stu.id + "'>修改</a>&nbsp;&nbsp;" +
            "<a href='#' onclick='deleteById(" + stu.id + ")'>删除</a>" +
            "</td>" +
            "</tr>";
    }
    $("tbody").html(html);
}

// 加载目录
function loadCatalogue() {
    // 目录跳转
    var html = "<a style=\"text-decoration: none;\" href='#' onclick=\"query(1,page.pageSize)\">首页</a>&nbsp;";
    if (page.pageIndex > 1) {
        html += "<a style=\"text-decoration: none;\" href='#'  onclick=\"query(page.pageIndex-1,page.pageSize)\">上一页</a>&nbsp;";
    }
    if (page.pageIndex > 4) {
        html += "...&nbsp;";
    }

    for (var i = (page.pageIndex - 3) < 1 ? 1 : page.pageIndex - 3; i > 0 && i < page.pageIndex; i++) {
        html += "<a style=\"text-decoration: none;\"  href='#' onclick=\"query(" + i + ",page.pageSize)\">" + i + "</a>&nbsp;";
    }
    html += "<a style=\"text-decoration: none;font-weight: bold;color: black\" href='#'  disabled>" + page.pageIndex + "</a>&nbsp;";
    for (var i = page.pageIndex + 1; i <= page.pageIndex + 3 && i <= page.totalPage; i++) {
        html += "<a style=\"text-decoration: none;\" href='#'  onclick=\"query(" + i + ",page.pageSize)\">" + i + "</a>&nbsp;";
    }
    if (page.pageIndex + 3 < page.totalPage) {
        html += "...&nbsp;";
    }
    if (page.pageIndex != page.totalPage) {
        html += "<a style=\"text-decoration: none;\" href='#'  onclick=\"query(page.pageIndex+1,page.pageSize)\">下一页</a>&nbsp;";
    }
    html += "<a style=\"text-decoration: none;\" href='#'  onclick=\"query(page.totalPage,page.pageSize)\">尾页</a>&nbsp;";
    html += "共" + page.total + "条&nbsp;";
    $("#catalogue").html(html);

    // 总页数
    $("#pageShow").html(page.pageIndex + "/" + page.totalPage);
}

// 拼接查询url
function url(pageIndex, pageSize) {
    return "/student?method=query&pageIndex=" + pageIndex + "&pageSize=" + pageSize
        + "&name=" + $("#name").val() + "&number=" + $("#number").val() + "&sex=" + $("#sex").val();
}

function formatDate(date) {//将当前时间转换成yyyy-mm-dd格式
    var str = "" + date.getFullYear() + "-";
    var mm = date.getMonth() + 1
    if (date.getMonth() > 9) {
        str += mm;
    } else {
        str += "0" + mm;
    }
    str += "-";
    if (date.getDate() > 9) {
        str += date.getDate();
    } else {
        str += "0" + date.getDate();
    }
    return str;
}

function deleteById(id) {
    ajaxGet("/student?method=delete&id=" + id, {}, function (data) {
        data = JSON.parse(data);
        if (data.success) {
            alert(data.msg);
            query(page.pageIndex, page.pageSize);
        } else {
            alert(data.msg);
        }
    });
}